<template>
  <a-card>
    <div class="table-page-search-wrapper">
      <a-form-model
        layout="inline"
        :model="queryParam"
      >
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-model-item label="合同编号">
              <a-input v-model="queryParam['filter[contract_no]']" placeholder="请输入合同编号" />
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-model-item label="客户电话">
              <a-input v-model="queryParam['filter[phone]']" placeholder="请输入客户电话" />
            </a-form-model-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务小类">
                <a-input v-model="queryParam['filter[business_name]']" placeholder="请输入业务小类" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务名称">
                <a-input v-model="queryParam['filter[tm_name]']" placeholder="请输入业务名称" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务类别">
                <a-select v-model="queryParam['filter[tm_type]']" placeholder="请选择业务类别">
                  <a-select-option value="1">商标注册【公司】</a-select-option>
                  <a-select-option value="2">商标注册【个人】</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="领取时间">
                <a-range-picker @change="onChangeTime" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="流程状态">
                <a-select v-model="queryParam['filter[status]']" placeholder="请选择流程状态">
                  <a-select-option value="1">待提交</a-select-option>
                  <a-select-option value="2">已提交</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <!--              <a-col :md="6" :sm="24">-->
            <!--                <a-form-item label="领取时间">-->
            <!--                  <a-range-picker @change="onChangeReceive" />-->
            <!--                </a-form-item>-->
            <!--              </a-col>-->
            <a-col :md="6" :sm="24">
              <a-form-model-item label="所属顾问">
                <a-select
                  show-search
                  v-model="queryParam['filter[adviser_uid]']"
                  placeholder="输入要搜索的顾问姓名"
                  :default-active-first-option="false"
                  :show-arrow="false"
                  :filter-option="false"
                  :not-found-content="null"
                  @search="onSearch"
                >
                  <a-select-option v-for="(item,index) in searchSource" :key="index" :value="index">
                    {{ item }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </template>
          <a-col :md="!advanced && 8 || 24" :sm="24">
                <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {}">
                  <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
                  <a-button style="margin-left: 8px" @click="reset">重置</a-button>
                  <a @click="toggleAdvanced" style="margin-left: 8px">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <div class="table-operator">
      <s-table
        bordered
        ref="table"
        :columns="materialColumns"
        :data="materialData"
      >
        <template slot="contract_no" slot-scope="text">
          <a-popover trigger="hover">
            <template slot="content">
              <p>合同编号：{{text}}<a-divider type="vertical" /><a-icon class="copy" :data-clipboard-text="text" type="copy" @click="copy" /></p>
            </template>
            {{text}}
          </a-popover>
        </template>
        <template slot="adviser" slot-scope="text, record">
          <a-tag>{{text}}</a-tag><br>
          <a-tag>{{record.group_type}}组</a-tag><br>
          <a-tag>{{record.leader}}</a-tag>
        </template>
        <template slot="phone" slot-scope="text">
          <a-popover trigger="hover">
            <template slot="content">
              <p>{{text}}<a-divider type="vertical" /><a-icon class="copy" :data-clipboard-text="text" type="copy" @click="copy" /></p>
            </template>
            {{text}}
          </a-popover>
        </template>
        <template slot="status" slot-scope="text, record">
          <a-button v-if="text == '待提交'" icon="edit" size="small" type="default" @click="changeStatus(record.cb_id)">{{text}}</a-button>
          <a-button v-if="text == '已提交'" icon="edit" size="small" type="primary" @click="changeStatus(record.cb_id)">{{text}}</a-button>
        </template>
        <template slot="history" slot-scope="text,record">
          <a @click="historyModal(record.cb_id)">{{text}}</a>
        </template>
        <template slot="action" slot-scope="text,record">
          <a-dropdown-button size="small" type="primary" @click="handleButtonClick(record.cb_id)">
            查看
            <a-menu slot="overlay" @click="(e) => handleMenuClick(e, '12')">
              <a-menu-item key="1"> <a-icon type="download" />下载</a-menu-item>
              <a-menu-item key="2"> <a-icon type="upload" />上传</a-menu-item>
            </a-menu>
          </a-dropdown-button>
        </template>
      </s-table>
    </div>
    <a-modal
      centered
      v-model="historyVisible"
      title="历史信息"
      :footer="null"
    >
      <a-timeline>
        <a-timeline-item>
          【提交材料到官方】，流程售后2，提交了材料到官方 2021-05-06 13:14:22
        </a-timeline-item>
        <a-timeline-item>
          【材料整理】，材料整理，整理材料 2021-05-06 12:55:04
        </a-timeline-item>
        <a-timeline-item>
          【提交材料】，孙臻，提交了材料 2021-05-06 11:47:29
        </a-timeline-item>
      </a-timeline>
    </a-modal>
    <a-modal
      centered
      v-model="submitVisible"
      title="编辑状态"
      @ok="submitChangeStatus"
      @cancel="onCancel"
    >
      <a-form-model
        ref="ruleForm"
        :model="changeStatusForm"
      >
        <a-form-model-item prop="status2">
          <a-radio-group @change="onChangeStatus">
            <a-radio value="1">
              材料反馈
            </a-radio>
            <a-radio value="2">
              材料已提交到官方
            </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item prop="notes">
          <a-textarea :rows="4" v-model="changeStatusForm.notes" placeholder="备注：材料反馈，请填写原因，不能为空"></a-textarea>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </a-card>
</template>

<script>
  import Clipboard from 'clipboard'
  import { STable } from '@/components'
  import { getMaterialTidy } from '@/api/tm.process'
  import trademarkColumns from '@/columns/tm.process'

  export default {
    components: {
      STable
    },
    name: 'TMMaterial',
    data () {
      return {
        advanced: true,
        queryParam: {},
        materialColumns: trademarkColumns.tmmaterial,
        historyVisible: false,
        submitVisible: false,
        materialData: parameters => {
          return getMaterialTidy(Object.assign(parameters, this.queryParam)).then(res => {
            return res.result
          })
        },
        searchSource: [],
        changeStatusForm: {}
      }
    },
    created () {
      this.queryParam['filter[category]'] = '2'
    },
    methods: {
      changeStatus (id) {
        const app = this
        app.submitVisible = true
      },
      submitChangeStatus () {
      },
      onCancel () {
        this.changeStatusForm = {}
        this.submitVisible = false
      },
      onChangeStatus (e) {
        console.log(e.target.value)
      },
      historyModal (id) {
        const app = this
        app.historyVisible = true
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      reset () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      onChangeTime (date, dateString) {
        this.queryParam['filter[open_time_start]'] = dateString[0]
        this.queryParam['filter[open_time_end]'] = dateString[1]
      },
      // onSearch (searchText) {
      //   onCompletionAdviser(searchText).then(res => {
      //     this.searchSource = res.result.data
      //   })
      // },
      copy () {
        const clipboard = new Clipboard('.copy')
        clipboard.on('success', e => {
          this.$message.success('复制成功')
        })
        clipboard.on('error', e => {
          this.$message.error('复制失败')
        })
      }
    }
  }
</script>

<style scoped>
  /deep/.table-operator button{margin-right: 0;}
</style>
